package cn.edu.xjtu.nbrash.xbarsuite.presms;

import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.RashIntents;
import cn.edu.xjtu.nbrash.xbarsuite.capture.CaptureActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class EditPreSms extends Activity {
	private boolean status = true; // 记录内容是否被更改，true表示没有更改，false表示已更改
	private PreSMS sms; // 短信在数据库中的id号
	private EditText theme, smsContent;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_presms);
		theme = (EditText) findViewById(R.id.presms_theme);
		smsContent = (EditText) findViewById(R.id.presms_text);
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		sms = (PreSMS) bundle.get("sms");
		status = true; // 表示没有作更改
		theme.setText(sms.getTitle());
		smsContent.setText(sms.getContent());
		theme.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				status = false;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});

		smsContent.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				status = false;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void back(View v) {
		if (status) {
			finish();
		} else {
			// 应该有个弹框判断是否退出
			// Toast.makeText(this.getBaseContext(), "还没有加弹框哦",
			// Toast.LENGTH_SHORT)
			// .show();
			new AlertDialog.Builder(this)
					.setTitle("确认")
					.setMessage("没有保存修改，确定退出吗？")
					.setPositiveButton("是",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// 点击“确认”后的操作
									finish();

								}
							}).setNegativeButton("否", null).show();
		}
	}

	public void save(View v) {
		if (status) {
			Toast.makeText(this.getBaseContext(), "没有做任何更改", Toast.LENGTH_SHORT)
					.show();
		} else {
			String content = smsContent.getText().toString();
			if (content.equals("")) {
				new AlertDialog.Builder(this).setTitle("提示")
						.setIcon(android.R.drawable.ic_dialog_info)
						.setMessage("短信内容不能为空").setPositiveButton("确定", null)
						.show();
				return;
			}
			if (theme.getText().toString().equals("")) {
				sms.setTitle(content.length() > 8 ? content.substring(0, 8)
						: content);
			} else {
				sms.setTitle(theme.getText().toString());
			}
			sms.setContent(smsContent.getText().toString());
			if (sms.isNewItem()) {
				PreSMS.addPreSMS(this, RashIntents.PreSMS.PRE_SMS_DATABASE, sms);
			} else {
				PreSMS.updatePreSMS(this, RashIntents.PreSMS.PRE_SMS_DATABASE,
						sms);
			}
			Toast.makeText(this.getBaseContext(), "保存成功", Toast.LENGTH_SHORT)
					.show();
			status = true;
		}
	}

	public void scanPhone(View v) {
		Intent intent = new Intent(this, CaptureActivity.class);
		intent.setAction(RashIntents.Scan.ACTION_PRE_SMS);
		intent.putExtra(RashIntents.Scan.EXTRA_PRE_SMS, sms.getContent());
		startActivity(intent);
	}
}
